package com.tianxiao.faas.application.web.corner;

import com.google.common.collect.Lists;
import com.tianxiao.faas.api.param.corner.ArticleResult;
import com.tianxiao.faas.api.param.corner.ArticleSaveParam;
import com.tianxiao.faas.api.param.corner.BasePageParam;
import com.tianxiao.faas.application.aop.AuthenticationContextHolder;
import com.tianxiao.faas.biz.service.corner.ArticleService;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/admin/article/")
public class ArticleAdminController {
    @Resource
    private ArticleService articleService;

    @RequestMapping(value = "/save")
    @Access(access = {"admin"}, action = "文章修改/插入")
    public Result<Boolean> save(@RequestBody ArticleSaveParam param) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        articleService.save(param, userId, 0);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/banner/save")
    @Access(access = {"admin"}, action = "文章修改/插入")
    public Result<Boolean> saveBanner(@RequestBody ArticleSaveParam param) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();

        articleService.save(param, userId, 2);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/publish")
    @Access(access = {"admin"}, action = "文章发布")
    public Result<Boolean> publish(Long id) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        articleService.publish(id, userId, 1);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/banner/publish")
    @Access(access = {"admin"}, action = "文章发布")
    public Result<Boolean> publishBanner(Long id) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        articleService.publish(id, userId, 3);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/list")
    @Access(access = {"admin"})
    public Result<List<ArticleResult>> list(@RequestBody BasePageParam param) {
        ArrayList<Integer> types = Lists.newArrayList(0, 1);
        List<ArticleResult> results = articleService.page(param, true, types);
        Integer count = articleService.count(param,types);
        return Result.builder().success(true).data(results).pageSize(param.getPageSize()).pageNo(param.getPage())
                .totalCount(count).totalPage();
    }

    @RequestMapping(value = "/banner/list")
    @Access(access = {"admin"})
    public Result<List<ArticleResult>> bannerList(@RequestBody BasePageParam param) {
        ArrayList<Integer> types = Lists.newArrayList(2, 3);
        List<ArticleResult> results = articleService.page(param, true, types);
        Integer count = articleService.count(param,types);
        return Result.builder().success(true).data(results).pageSize(param.getPageSize()).pageNo(param.getPage())
                .totalCount(count).totalPage();
    }

    @RequestMapping(value = "/banner/delete")
    @Access(access = {"admin"}, action = "banner文章删除")
    public Result<ArticleResult> deleteBanner(Long id) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        articleService.delete(id, userId);
        return Result.builder().success(true).data(true);
    }

    @RequestMapping(value = "/get")
    @Access(access = {"admin"})
    public Result<ArticleResult> get(Long id) {
        ArticleResult results = articleService.get(id);
        return Result.builder().success(true).data(results);
    }

    @RequestMapping(value = "/delete")
    @Access(access = {"admin"}, action = "文章删除")
    public Result<ArticleResult> delete(Long id) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        articleService.delete(id, userId);
        return Result.builder().success(true).data(true);
    }
}
